package com.cjy.expenses.action;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.cjy.common.bean.OpAttributes;
import com.cjy.expenses.service.ExpensesService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.annotation.Resource;

import java.text.DecimalFormat;
import java.util.Arrays;

import static com.base.GlobaConstants.*;

@Action("expensesAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT, ACTION_RESULT_DATA, CONTENT_TYPE,
		CONTENT_TYPE_JSON }) })
public class ExpensesAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private ExpensesService expensesService;

	private int bid; //
	private String body; //
	private String categories; // 费用范围
	private String code;
	private int compoundsId; // 当前房屋所属物业ID
	private String endTime; // 格式为 yyyy-MM
	private int fee; // 费用
	private double money;
	private String out_trade_no; // 订单号
	private String ResID;
	private String revFeeIdsArr; // 费用主键ID数组
	private String startTime; // 格式为 yyyy-MM
	private int terminalId;
	private int userId;
	private int page;
	private int transactionId;
	private String unJson;
	private String txnTime; // 订单发送时间

	// 商城订单参数
	private String username;
	private String phone;
	private int userType;	// 0 会员 1 游客
	private String address;
	private Long receivingInfoId;
    private String out_trade_nos_str;
    private String cuname;

	public String getCuname() {
		return cuname;
	}

	public void setCuname(String cuname) {
		this.cuname = cuname;
	}

	public String getOut_trade_nos_str() {
        return out_trade_nos_str;
    }

    public void setOut_trade_nos_str(String out_trade_nos_str) {
        this.out_trade_nos_str = out_trade_nos_str;
    }

	public String getUnJson() {
		return unJson;
	}

	public void setUnJson(String unJson) {
		this.unJson = unJson;
	}

	public String getTxnTime() {
		return txnTime;
	}

	public void setTxnTime(String txnTime) {
		this.txnTime = txnTime;
	}

	public int getBid() {
		return bid;
	}

	public String getBody() {
		return body;
	}

	public String getCategories() {
		return categories;
	}

	public String getCode() {
		return code;
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public String getEndTime() {
		return endTime;
	}

	public int getFee() {
		return fee;
	}

	public double getMoney() {
		return money;
	}

	public String getOut_trade_no() {
		return out_trade_no;
	}

	public String getResID() {
		return ResID;
	}

	public String getRevFeeIdsArr() {
		return revFeeIdsArr;
	}

	public String getStartTime() {
		return startTime;
	}

	public int getTerminalId() {
		return terminalId;
	}

	public int getUserId() {
		return userId;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public void setCategories(String categories) {
		this.categories = categories;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void setFee(int fee) {
		this.fee = fee;
	}

	public void setMoney(double money) {
		this.money = money;
	}

	public void setOut_trade_no(String out_trade_no) {
		this.out_trade_no = out_trade_no;
	}

	public void setResID(String resID) {
		ResID = resID;
	}

	public void setRevFeeIdsArr(String revFeeIdsArr) {
		this.revFeeIdsArr = revFeeIdsArr;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setTerminalId(int terminalId) {
		this.terminalId = terminalId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTransactionId() {
		return transactionId;
	}

	public void setTransactionId(int transactionId) {
		this.transactionId = transactionId;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public int getUserType() {
		return userType;
	}

	public void setUserType(int userType) {
		this.userType = userType;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Long getReceivingInfoId() {
		return receivingInfoId;
	}

	public void setReceivingInfoId(Long receivingInfoId) {
		this.receivingInfoId = receivingInfoId;
	}

	/**
	 * 商城获取支付宝预支付信息
	 *

	 * @return
	 */
	public String getShopAlipayInfo() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",
					expensesService.getShopAlipayInfo(fee,username,phone,address,code,userType,revFeeIdsArr, cuname)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 获取支付宝预支付信息
	 *
	 * @return
	 */
	public String getAlipayInfo() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",
					expensesService.getAlipayInfo(fee, revFeeIdsArr, code, userId, bid)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 获取交易信息列表
	 * 
	 * @return
	 */
	public String queryPayInfoList() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",
					expensesService.queryPaidInfoList(userId, code, bid, startTime, endTime, page)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 获取交易信息列表条数
	 * 
	 * @return
	 */
	public String queryPayInfoListCounts() {
		try {
			actionResult = ActionResult.getActionResult(
					new OpAttributes().add("code", 1).add("result", new OpAttributes().add("totalPages",
							expensesService.queryPaidInfoListCounts(userId, code, bid, startTime, endTime))));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 获取交易信息详细列表
	 * 
	 * @return
	 */
	public String queryPayInfoDetailList() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",
					expensesService.queryPaidInfoDetail(transactionId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 获取缴费信息
	 * 
	 * @return
	 */
	public String getExpenses() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",
					expensesService.getExpensesInfo(ResID, startTime, endTime, compoundsId, categories)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 获取微信prepayId
	 * 
	 * @return
	 */
	public String getWeinxinPayInfo() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",
					expensesService.getWeixinPayInfo(fee, revFeeIdsArr, code, userId, bid, body)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String getShopWeinxinPayInfo() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",
					expensesService.getShopWeixinPayInfo(fee,username,phone,address,userType,revFeeIdsArr,code, body, cuname)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 获取银联订单TN号
	 * 
	 * @return
	 */
	public String getUnionpayInfo() {
		try {
			System.out.println("--------------");
			
//			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",expensesService.getUnionpayInfo(fee, revFeeIdsArr, code, userId, bid, body)
					));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String getShopUnionpayInfo() {
		try {
			System.out.println("--------------");

//			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result",expensesService.getShopUnionpayInfo(fee,username,phone,address,userType, revFeeIdsArr, code, body, cuname)
			));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 获取微信订单状态
	 * 
	 * @return
	 */
	public String queryWeinxinOrderStatus() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes().add("code", expensesService.queryOrderStatus(out_trade_no)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String queryShopWeinxinOrderStatus() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes().add("code", expensesService.queryShopOrderStatus(out_trade_no)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String subPaied() {
		try {
			actionResult = ActionResult.getActionResult(
					new OpAttributes().add("code", 1).add("result", expensesService.subPaied(userId, money)));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 银联验签
	 * 
	 * @return
	 */
	public String unionpayVerifyAppData() {
		try {
			actionResult = new ActionResult(
					new OpAttributes().add("code", expensesService.unionpayVerifyAppData(unJson)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String shopUnionpayVerifyAppData() {
		try {
			actionResult = new ActionResult(
					new OpAttributes().add("code", expensesService.shopUnionpayVerifyAppData(unJson)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String queryUnionpayOrderStatus() {
		try {
			actionResult = new ActionResult(
					new OpAttributes().add("code", expensesService.queryUnionpayOrderStatus(out_trade_no, txnTime)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String queryShopUnionpayOrderStatus() {
		try {
			actionResult = new ActionResult(
					new OpAttributes().add("code", expensesService.queryShopUnionpayOrderStatus(out_trade_no, txnTime)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	
	public String addShopUserInfo() {
		try {
			actionResult = new ActionResult(
					new OpAttributes().add("code", expensesService.addShopUserInfo(username,phone,address,code,userType)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	
	public String queryShopUserReceivingInfo(){
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", expensesService.queryShopUserReceivingInfo(code)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String updateShopUserReceivingInfo(){
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", expensesService.updateShopUserReceivingInfo(username,phone,address,receivingInfoId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String deleteShopUserReceivingInfo() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", expensesService.deleteShopUserReceivingInfo(receivingInfoId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String queryShopScore() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code",1).add("result", expensesService.queryShopScore(code)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", 0));
		}
		return returnType();
	}

	public String queryShopOrderByTerminalCode() {
        try {
            actionResult = new ActionResult(new OpAttributes().add("code",1).add("result", expensesService.queryShopOrderByTerminalCode(code)));
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = new ActionResult(new OpAttributes().add("code", 0));
        }
        return returnType();
	}

    public String queryShopOrderByStatus(){
        try {
            actionResult = new ActionResult(expensesService.queryShopOrderByStatus());
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = new ActionResult(new OpAttributes().add("code", 0));
        }
        return returnType();
    }

    public String delivery() {
        try {
            String[] out_trade_noArr = out_trade_nos_str.split(",");
            Arrays.asList(out_trade_noArr).stream().parallel().forEach((item) -> actionResult = new ActionResult(new OpAttributes().add("code", expensesService.updateShopOrderStatus(item.trim(), 2))));
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = new ActionResult(new OpAttributes().add("code", 0));
        }
        return  returnType();
    }

	public String confirmReceipt() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", expensesService.updateShopOrderStatus(out_trade_no, 3)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public static void main(String[] args) {
		double a = 1050.11;
		DecimalFormat decimalFormat = new DecimalFormat("0");
		System.out.println(decimalFormat.format(a*100));
	}
}
